package com.finger.crowd.service.api.impl;

import com.finger.crowd.entity.Role;
import com.finger.crowd.entity.RoleExample;
import com.finger.crowd.mapper.RoleMapper;
import com.finger.crowd.service.api.RoleService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleMapper roleMapper;

    @Override
    public PageInfo<Role> getPageInfoForRole(Integer pageNum, Integer pageSize, String keyword) {
        PageHelper.startPage(pageNum, pageSize);
        List<Role> roles = roleMapper.selectRoleByKeyword(keyword);
        return new PageInfo<Role>(roles);
    }

    @Override
    public int saveRole(String roleName) {
        Role role = new Role();
        role.setName(roleName);
        int i = roleMapper.insertSelective(role);
        return i;
    }

    @Override
    public Role selectRoleById(Integer id) {
        return roleMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateRole(Role role) {
        return roleMapper.updateByPrimaryKeySelective(role);
    }

    @Override
    public int remove(List<Integer> ids) {
        RoleExample roleExample = new RoleExample();
        RoleExample.Criteria criteria = roleExample.createCriteria();
        criteria.andIdIn(ids);
        int resultCounts = roleMapper.deleteByExample(roleExample);
        return resultCounts;
    }

    @Override
    public List<Role> selectAssignedByAdminId(Integer adminId) {

        return roleMapper.selectAssignedRoles(adminId);
    }

    @Override
    public List<Role> selectUnAssignedByAdminId(Integer adminId) {
        return roleMapper.selectUnAssignedRole(adminId);
    }
}
